package com.s3.demo.controller;

import com.s3.demo.service.AuthService;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.Enumeration;

/**
 * @author lzz
 * @create 2021-09-06 10:46
 */
@RequestMapping("/")
@Controller
public class S3Controller {

    @Resource
    AuthService authService;

    @PutMapping("/{bucketName}/{fileName}")
    public void upload(HttpServletRequest request, HttpServletResponse response, @PathVariable("bucketName") String bucketName, @PathVariable("fileName") String fileName,MultipartFile file) throws NoSuchAlgorithmException, IOException {
        System.out.println("upload - bucketName = " + bucketName);
        System.out.println("upload - fileName = " + fileName);
        System.out.println("upload - test");

        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String name = headerNames.nextElement();
            String header = request.getHeader(name);
            System.out.println("upload - " + name + " : " + header);
        }

        Enumeration<String> parameterNames = request.getParameterNames();
        while (parameterNames.hasMoreElements()) {
            String name = parameterNames.nextElement();
            String parameter = request.getParameter(name);
            System.out.println("upload - parameter : " + name + " : " + parameter);
        }
        String authorization = request.getHeader("authorization");
        if (!StringUtils.isEmpty(authorization)) {
            authService.doUpload(request, bucketName, fileName);
        }else{
            //预签名url 上传文件
            authService.preSignDoUpload(request, bucketName, fileName,file);
        }


    }




    @GetMapping("/{bucketName}/{fileName}")
    public void download(HttpServletRequest request, HttpServletResponse response, @PathVariable("bucketName") String bucketName, @PathVariable("fileName") String fileName) throws NoSuchAlgorithmException {
        System.out.println("download - bucketName = " + bucketName);
        System.out.println("download - fileName = " + fileName);
        System.out.println("download - test");

        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String name = headerNames.nextElement();
            String header = request.getHeader(name);
            System.out.println(name + " : " + header);
        }
        authService.doDownload(bucketName,fileName,request,response);
    }
}
